package logic.common.game.endConditions;

import logic.common.game.Game;
import logic.common.player.Player;
import logic.common.team.Team;
import logic.common.team.teamListeners.DroneKillEvent;
import logic.common.team.teamListeners.TeamListener;
import logic.common.team.teamListeners.TeamValueChangedEvent;

public class TicketsCondition extends MatchEndCondition {
	
	private boolean isEnding;
	
	public TicketsCondition(Game game) {
		super(game);
		
		addTeamScoreListener(game.getTeamA());
		addTeamScoreListener(game.getTeamB());
	}
	
	protected void addTeamScoreListener(final Team team) {
		team.addTeamListener(new TeamListener() {
			@Override
			public void teamValueChanged(TeamValueChangedEvent event) {
				if(team.getTickets() <= 0 && !isEnding) {
					isEnding = true;
					endGame(team.getEnemyTeam());
				}
			}
			
			@Override
			public void playerRemoved(Player player) {}
			@Override
			public void playerAdded(Player player) {}
			@Override
			public void droneDestroyed(DroneKillEvent event) {}
		});
	}
}